/*
 * Copyright (c) 2011 Norbert Kopcsek
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.modulr.feature {

	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	[Event( name="complete", type="flash.events.Event" )]
	[Event( name="error", type="flash.events.ErrorEvent" )]
	/**
	 * Responsible for loading a single feature.
	 * Throw away objects, no need to clean up event listeners inside this class.
	 *
	 * @see org.modulr.feature.Feature
	 *
	 * @author Norbert Kopcsek
	 */
	public class FeatureLoader extends EventDispatcher {

		//------------------------------------------------------------------------------
		//
		//   Constructor 
		//
		//------------------------------------------------------------------------------

		public function FeatureLoader( feature:Feature ) {
			_feature = feature;
		}

		//------------------------------------------------------------------------------
		//
		//   Properties (Getter/Setter) 
		//
		//------------------------------------------------------------------------------

		//--------------------------------------
		// feature 
		//--------------------------------------

		/**
		 * The feature provided in the constructor.
		 */
		public function get feature():Feature {
			return _feature;
		}

		//------------------------------------------------------------------------------
		//
		//   Protected fields 
		//
		//------------------------------------------------------------------------------

		/**
		 * @private
		 */
		protected var _feature:Feature;

		/**
		 * Loader used to load the bundles xml file.
         * @private
		 */
        // TODO Compiler says this field can be converted to a local variable, but it will probably be garbage collected ...
		private var loader:URLLoader;

		//------------------------------------------------------------------------------
		//
		//  Methods 
		//
		//------------------------------------------------------------------------------

		/**
		 * Loads the bundle xml provided by the feature.
		 */
		public function load():void {
			loader = new URLLoader();
			loader.data = feature;
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener( Event.COMPLETE, loader_completeHandler ); // no weak reference
			loader.addEventListener( IOErrorEvent.IO_ERROR, loader_ioErrorHandler ); // no weak reference
			loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler ); // no weak reference
			loader.load( new URLRequest( feature.url ));
		}

		//--------------------------------------------------------------------------
		//
		//  Event listeners
		//
		//--------------------------------------------------------------------------
		/**
		 * @private
		 */
		protected function loader_completeHandler( event:Event ):void {
			// TODO Need to check if this is proper xml
			// TODO Test this stuff
			feature.bundlesXml = new XML( event.target.data )
			dispatchEvent( new Event( Event.COMPLETE ));
		}

		/**
		 * @private
		 */
		protected function loader_ioErrorHandler( event:Event ):void {
			dispatchEvent( new ErrorEvent( ErrorEvent.ERROR ));
		}

		/**
		 * @private
		 */
		protected function loader_securityErrorHandler( event:Event ):void {
			dispatchEvent( new ErrorEvent( ErrorEvent.ERROR ));
		}
	}
}

